<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
             label-width="68px">
      <el-form-item label="订单编号" prop="orderCode">
        <el-input v-model="queryParams.orderCode" placeholder="请输入订单编号"
                  clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="送货地址" prop="deliveryAddress">
        <el-input v-model="queryParams.deliveryAddress" placeholder="请输入送货地址"
                  clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="联系人" prop="contacts">
        <el-input v-model="queryParams.contacts" placeholder="请输入联系人"
                  clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="交货日期" prop="deliveryDate">
        <el-date-picker clearable v-model="queryParams.deliveryDate"
                        type="date" value-format="yyyy-MM-dd" placeholder="请选择交货日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table border v-loading="loading" :data="dataList" @selection-change="handleSelectionChange" ref="table"
              :max-height="tableMaxHeight">
      <el-table-column label="框架编号" align="center" prop="project.code" width="150"/>
      <el-table-column label="订单编号" align="center" prop="order.code"/>
      <el-table-column label="拆分编号" align="center" prop="splitCode"/>
      <el-table-column label="金额" align="center" prop="money"/>
      <el-table-column label="送货地址" align="center" prop="deliveryAddress" show-overflow-tooltip width="200"/>
      <el-table-column label="联系人" align="center" prop="contacts"/>
      <el-table-column label="联系电话" align="center" prop="phone" width="110"/>
      <el-table-column label="制单人" align="center" prop="createBy" width="110"/>
      <el-table-column label="交货日期" align="center" prop="deliveryDate" width="100">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.deliveryDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="总重量" align="center" prop="weight"/>
      <el-table-column label="是否置顶" align="center" key="status">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.isTop"
            active-value="0"
            inactive-value="1"
            @change="handleIsTopChange(scope.row)"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column align="center" label="状态" prop="status" width="120">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.order_iron_part_status" :value="scope.row.status"/>
        </template>
      </el-table-column>

      <el-table-column align="center" label="是否采购" prop="buyStatus" fixed="right">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.yes_no" :value="scope.row.buyStatus"/>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="250" fixed="right">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit"
                     @click="handleUpdate(scope.row)" v-hasPermi="['develop:OrderIronRecord:edit']">修改
          </el-button>

          <el-button size="mini" type="text" icon="el-icon-s-operation"
                     @click="handleDetailViewList(scope.row)" v-hasPermi="['develop:OrderIronRecord:edit']">查看明细
          </el-button>

          <el-button size="mini" type="text" icon="el-icon-s-operation" v-if="scope.row.status === '0' || scope.row.status === '3'"
                     @click="handleDetailList(scope.row)" v-hasPermi="['develop:OrderIronRecord:edit']">补充明细
          </el-button>

          <el-button size="mini" type="text" icon="el-icon-s-finance" v-if="scope.row.status === '1' || scope.row.status === '3'"
                     @click="handleApplyMaterial(scope.row)" v-hasPermi="['develop:OrderIronRecord:edit']">材料请购
          </el-button>

          <el-popconfirm :title="`确认退回补充？`" @confirm="handleReturnEdit(scope.row)" v-if="scope.row.status === '1'"
                         style="margin-left: 10px;">
            <el-button slot="reference" size="mini" type="text" icon="el-icon-remove-outline"
                        v-hasPermi="['develop:OrderIronRecord:edit']">退回补充
            </el-button>
          </el-popconfirm>

          <el-popconfirm :title="`确认补充完毕？`" @confirm="handleFinish(scope.row)" v-if="scope.row.status === '0'"
                         style="margin-left: 10px;">
            <el-button slot="reference" size="mini" type="text" icon="el-icon-success"
                       v-hasPermi="['develop:OrderIronRecord:edit']">补充完毕
            </el-button>
          </el-popconfirm>

          <el-popconfirm :title="`确认导出当前数据项？`" @confirm="handleExportDetail(scope.row)"
                         style="margin-left: 10px;">
            <el-button slot="reference" size="mini" type="text" icon="el-icon-download"
                       v-hasPermi="['develop:OrderIronRecord:export']">导出
            </el-button>
          </el-popconfirm>

          <el-button size="mini" type="text" icon="el-icon-tickets" v-if="scope.row.buyStatus === '0'"
                     @click="handleBuy(scope.row)" v-hasPermi="['develop:OrderIronRecord:edit']">申请采购
          </el-button>


        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum"
                :limit.sync="queryParams.pageSize" @pagination="getList"/>
    <orderIronRecord-form ref="form" @ok="getList"></orderIronRecord-form>
    <order-iron-detail-list-module ref="detailList"></order-iron-detail-list-module>
    <order-iron-detail-list-view-module ref="detailViewList"></order-iron-detail-list-view-module>
    <goods-orderin-chose @ok="getList" ref="formChoseOther"></goods-orderin-chose>
    <goods-sale-material-please-buy-list  @ok="getList" ref="materialBuy"></goods-sale-material-please-buy-list>
    <lv-buy-iron-form ref="buyIronForm" @ok="getList"></lv-buy-iron-form>
  </div>
</template>

<script>
import {deleteAction, getAction, updateAction,downloadFile} from '@/api/manage'
import OrderIronRecordForm from '@/views/develop/orderIronRecord/module/OrderIronRecordForm'
import OrderIronDetailList from "@/views/develop/orderIronDetail/OrderIronDetailList.vue";
import OrderIronDetailListModule from "@/views/develop/orderIronDetail/module/OrderIronDetailListModule.vue";
import OrderIronDetailListViewModule from "@/views/develop/orderIronDetail/module/OrderIronDetailListViewModule.vue";
import GoodsOrderinChose from "@/views/develop/pleaseBuy/prjForGoodsIn/GoodsBuyChose.vue";
import {changeUserStatus} from "@/api/system/user";
import GoodsSaleMaterialPleaseBuyList
  from "@/views/develop/goodsSaleMaterial/module/GoodsSaleMaterialPleaseBuyList.vue";
import LvBuyIronForm from "@/views/develop/lvBuy/module/LvBuyIronForm.vue";

export default {
  name: "OrderIronRecordListModule",
  dicts: ["order_iron_part_status","yes_no"],
  components: {
    LvBuyIronForm,
    GoodsSaleMaterialPleaseBuyList,
    GoodsOrderinChose,
    OrderIronDetailListViewModule,
    OrderIronDetailListModule,
    OrderIronDetailList,
    OrderIronRecordForm,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 铁附件订单拆解记录表格数据
      dataList: [],
      orderId: null,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderId: null,
        orderCode: null,
        deliveryAddress: null,
        contacts: null,
        phone: null,
        deliveryDate: null,
        weight: null,
        orderByColumn: "isTop asc,create_time", // 排序字段
        isAsc: "desc", // 排序方式
      },
      url: {
        list: '/orderIronRecord/orderIronRecord/list',
        edit: '/orderIronRecord/orderIronRecord/edit',
        remove: '/orderIronRecord/orderIronRecord/',
        exportDetailList: '/orderIronRecord/orderIronRecord/exportDetailList',
      },
      tableMaxHeight: window.innerHeight - 320,
    };
  },
  created() {
    this.getList();
    window.addEventListener('resize', this.getTableHeight);
  },
  destroyed() {
    window.removeEventListener('resize', this.getTableHeight)
  },
  methods: {
    /** 查询铁附件订单拆解记录列表 */
    getList() {
      this.loading = true;
      getAction(this.url.list, this.queryParams).then(response => {
        this.dataList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getTableHeight() {
      this.tableHeight = window.innerHeight - 320
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$refs.form.handleAdd(this.orderId);
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.$refs.form.handleUpdate(row);
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      deleteAction(this.url.remove, ids).then(response => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(this.url.export, {
        ...this.queryParams
      }, `orderIronRecord_${new Date().getTime()}.xlsx`)
    },

    show(orderId) {
      this.open = true;
      this.orderId = orderId;
      this.queryParams.orderId = orderId;
      this.getList();
    },

    handleDetailList(row) {
      this.$refs.detailList.show(row.id, row.orderId);
    },
    handleDetailViewList(row) {
      this.$refs.detailViewList.show(row.id, row.orderId);
    },

    handleExportDetail(row) {
      let that = this;
      let params = {"id": row.id};
      downloadFile(that.url.exportDetailList, params);
    },

    handleFinish(row){
      let that = this;
      let params = {"id": row.id,"status":"1"};
      updateAction(that.url.edit, params).then(response => {
        this.getList();
        this.$modal.msgSuccess("补充完成操作成功");
      }).catch(() => {
      });
    },

    handleReturnEdit(row){
      let that = this;
      let params = {"id": row.id,"status":"0"};
      updateAction(that.url.edit, params).then(response => {
        this.getList();
        this.$modal.msgSuccess("退回修改操作成功");
      }).catch(() => {
      });
    },

    handleApplyMaterial(row){
      this.$refs.materialBuy.pleaseBuyByIronRecord(row.id);
    },

    // 是否置顶修改
    handleIsTopChange(row) {
      let that = this;
      let text = row.isTop === "0" ? "置顶" : "取消置顶";
      this.$modal.confirm('确认要"' + text + '""' + row.splitCode + '"订单吗？').then(function () {
        let params = {"id": row.id,"isTop":row.isTop};
        updateAction(that.url.edit, params).then(response => {
          that.getList();
        }).catch(() => {
        });
      }).then(() => {
        this.$modal.msgSuccess(text + "成功");
      }).catch(function () {
        row.isTop = row.isTop === "0" ? "1" : "0";
      });
    },

    handleBuy(row){
      this.$refs.buyIronForm.handleAddBuyIronRecord(row);
    },


  }
};
</script>
